﻿@using HmPMer.Business
@model HmPMer.Entity.UserBaseInfo
@{
    ViewBag.Title = "结算申请";
    ViewBag.PageUri = "/Withdraw/WithdrawApply";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    var withdrawChannelList = new HmPMer.Business.WithdrawBll().GetWithdrawChannelList(1);
    var dicList = new HmPMer.Business.DistrictBll().LoadParentId(0);
    var banklist = new HmPMer.Business.UserBankBll().GetBankList(HmPMer.MerUI.Models.ModelCommon.GetUserModel().UserId);
    var isSmsCheck = new SysConfigBll().GetForKey("IsSmsCheck");
}

<link href="~/Content/jquery.editable-select.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery.editable-select.min.js?111111111111111"></script>
<div class="container-fluid">
    <div id="pad-wrapper" class="form-page">

        <div class="row-fluid head">
            <div class="span12">
                <h4>代付申请</h4>
            </div>
        </div>
        <form id="formdata">
            <div class="row-fluid withdraw">

                <div class="field-box withdrawApply">
                    <label>商户ID: </label>@Model.UserId
                </div>
                <div class="field-box withdrawApply">
                    @Html.Hidden("Balance", Model.Balance / 100)
                    <label>可用余额: </label>
                    <b>￥@(Model.Balance / 100) </b>
                </div>

                <div class="field-box">
                    <label>账户类型</label>
                    <input style="margin-bottom:5px;width:30px;height:15px;" type="radio" name="AccountType" value="0" checked />个人
                    <input style="margin-bottom:5px;width:30px;height:15px;" type="radio" name="AccountType" value="1" />企业
                </div>

                <div class="field-box withdrawApply">
                    <label>银行账户</label>
                    @*<input class="span3" type="text" id="BankCode" name="BankCode" value="" onkeyup="this.value=this.value.replace(/[^\d{1-9}{1}\d{0-9}]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d{1-9}{1}\d{0-9}]/g,'')" />*@

                    <select id="BankCode" name="BankCode" style="width:300px">
                        <option value=""></option>
                        @foreach (var item in banklist)
                        {
                            <option value="@item.BankCode">@( item.BankName + "-" + item.BankCode + "-" + item.FactName)</option>
                        }
                    </select>
                </div>

                <div class="field-box withdrawApply">
                    <label>收款人</label>
                    <input class="span2" type="text" id="FactName" name="FactName" value="" />
                </div>

                <div class="field-box withdrawApply">
                    <label>预留手机号</label>
                    <input class="span2" type="text" id="ReservedPhone" name="ReservedPhone" onkeyup="this.value=this.value.replace(/[^\d{1-9}{1}\d{0-9}]/g,'')"
                           onafterpaste="this.value=this.value.replace(/[^\d{1-9}{1}\d{0-9}]/g,'')" value="" />
                </div>

                <div class="field-box withdrawApply">
                    <label>开户银行：</label>
                    <div class="ui-select" style="width:275px;">
                        <select id="WithdrawChannelCode" name="WithdrawChannelCode">
                            <option value="">请选择开户行</option>
                            @foreach (var item in withdrawChannelList)
                            {
                                <option value="@item.Code">@item.Name</option>
                            }
                        </select>
                    </div>
                    <span class="alert-msg" id="msg_WithdrawChannelCode"></span>
                </div>

                <div class="field-box withdrawApply">
                    <label>开户省市：</label>
                    <div class="ui-select" style="width:135px;">
                        <select id="ProvinceId" name="ProvinceId">
                            <option value="">请选择</option>
                            @foreach (var item in dicList)
                            {
                                <option value="@item.Id">@item.Name</option>
                            }
                        </select>
                    </div>

                    <div class="ui-select" style="width:135px;">
                        <select id="CityId" name="CityId">
                            <option value="">请选择</option>
                        </select>
                    </div>
                    <span class="alert-msg" id="msg_Province"></span>
                </div>

                <div class="field-box withdrawApply">
                    <label>开户支行</label>
                    @*<select id="BankLasalleCode" name="BankLasalleCode" style="width:300px;">
                <option value="">请选择</option>
            </select>*@

                    <div class="ui-select" style="width:275px;">
                        <select id="BankLasalleCode" name="BankLasalleCode">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>


                <div class="input-append withdrawApply">
                    <label>提现金额</label>
                    <input class="input-large" type="text" id="WithdrawAmt" name="WithdrawAmt" onkeyup="this.value=this.value.replace(/[^\d{1-9}{1}\d{0-9}]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d{1-9}{1}\d{0-9}]/g,'')" />
                    <span class="add-on">.00</span>
                </div>

                <div class="field-box withdrawApply">
                    <label>手续费</label>
                    <b>￥<span id="Handing">0.00</span></b>
                </div>
                @if (isSmsCheck.Value.Equals("1"))
                {
                    <div class="field-box withdrawApply">
                        <label>验证码:</label>
                        <input class="span2 inline-input" placeholder="请输入您手机收到的验证码" type="text" name="PhoneCode" id="PhoneCode"/>
                        <span class="btn-flat white" id="SendSmsCode">获取验证码</span>
                    </div>
                }
                else
                {
                    <div class="field-box withdrawApply">
                        <label>验证码:</label>
                        <input class="span2 inline-input" placeholder="请输入右则图形验证码" type="text" name="ImgCode" id="ImgCode" maxlength="4" />
                        <img id="img_code" src="/DrawingApi/GetValidateImg?codeKey=imgagentwithdrawapply" alt="" onclick="this.src = '/DrawingApi/GetValidateImg?codeKey=imgagentwithdrawapply&t=' + Math.random();" height="20" style="cursor:pointer" />
                    </div>
                }

                @*<div class="field-box withdrawApply">
            <label>手机号码</label> @Model.MobilePhone <span class="btn-flat white" id="GetSmsCode">获取验证码</span>
        </div>

        <div class="field-box withdrawApply">
            <label>短信验证码:</label>
            <input class="span2 inline-input" placeholder="点击上方按钮获取短信验证码" id="PhoneCode" name="PhoneCode" type="text" />
        </div>*@

                <div class="field-box withdrawApply">
                    <label>提现密码:</label>
                    <input class="span2 inline-input" id="Pass2" name="Pass2" type="password" value="" />
                </div>

                <div class="span11 field-box actions">

                    @if (Model.IsMobilePhone == 1)
                    {

                        if (Model.IdCardStatus == 1)
                        {
                            <label></label>
                            <input type="button" class="btn-glow primary" value="确认提交" id="btn_submit">
                        }
                        else
                        {
                            if (Model.AccountType == 0)
                            {
                                <span style="color:red;font-size:16px;margin-left:20px;">请先进行实名认证</span><a href="/Center/AuthIdCard" style="font-size:16px;margin-left:5px;">去认证</a>
                            }
                            else
                            {
                                <span style="color:red;font-size:16px;margin-left:20px;">请先进行企业认证</span><a href="/Center/AuthCompany" style="font-size:16px;margin-left:5px;">去认证</a>
                            }
                        }
                    }
                    else
                    {
                        <span style="color:red;font-size:16px;margin-left:20px;">请先进行手机认证</span><a href="/Center/AuthPhone" style="font-size:16px;margin-left:5px;">去认证</a>
                    }
                </div>

            </div>
        </form>
    </div>
</div>

<script>

    function loadCity(pid,cityid) {
        $.HM.ajax("base/loadcitylist/" + pid,
            {
                data: { 'ts': (new Date().getTime()) },
                type: "GET",
                dataType: 'json',
                load: false,
                success: function (d) {
                    if (d.length == 0) {
                        $("#CityId").empty().append('<option value="">请选择</option>');
                        return;
                    }
                    var _html = '';
                    _html += '<option value="">请选择</option>';
                    $.each(d.data, function (i, v) {
                        if (cityid == v.Id) {
                            _html += '<option value="' + v.Id + '" selected>' + v.Name + '</option>';
                        } else {
                            _html += '<option value="' + v.Id + '">' + v.Name + '</option>';
                        }
                    });
                    $("#CityId").empty().append(_html);
                }
            });
    }

    function loadBank(BankCode, Proid, Cityid, BankLasalleCode) {
        $.HM.ajax("base/LoadBank",
            {
                data: { BankCode: BankCode, Proid: Proid, Cityid: Cityid, 'ts': (new Date().getTime()) },
                type: "GET",
                dataType: 'json',
                load: false,
                success: function (d) {
                    if (d.length == 0) {
                        $("#BankLasalleCode").empty().append('<option value="">请选择</option>');
                        return;
                    }
                    var _html = '';
                    _html += '<option value="">请选择</option>';
                    $.each(d.data, function (i, v) {
                        if (v.BankLasalleCode == BankLasalleCode) {
                            _html += '<option value="' + v.BankLasalleCode + '" selected>' + v.BankLasalleName + '</option>';
                        } else {
                            _html += '<option value="' + v.BankLasalleCode + '">' + v.BankLasalleName + '</option>';
                        }

                    });
                    $("#BankLasalleCode").empty().append(_html);

                }
            });
    }

    function SetTips(id, meg) {
        $("#" + id).next("span").html('<i class="icon-remove-sign"></i>' + meg);
        $("#" + id).next("span").show();
        $("#" + id).focus();

    }

    function getWithdrawHanding(amount) {
        if (amount == '') {
            $("#Handing").val('0');
            return;
        }

        $.HM.ajax("Withdraw/GetWithdrawHanding", {
            data: { 'amount': amount, 'ts': (new Date().getTime()) },
            type: "GET",
            dataType: 'json',
            load: false,
            success: function (d) {
                if (!d.IsSuccess) {
                    $("#Handing").html('0');
                } else {
                    $("#Handing").html(d.data);

                    var balance = parseFloat($.trim($("#Balance").val()));

                    if (parseFloat(amount) + d.data > balance) {
                        layer.msg("余额不够，请修改提现金额", { icon: 0 });
                        $("#WithdrawAmt").focus();
                        return;

                    }
                }
            }
        });
    }

    $(function () {

        $('#BankCode').editableSelect({
            effects: 'slide',
            filter: true,
            onSelect: function () {
                if (this.val() != "") {
                    $.HM.ajax("Withdraw/GetBankForCode", {
                        data: { BankCode: this.val()},
                        type: "POST",
                        dataType: 'json',
                        success: function (d) {
                            if (d) {
                                $("#FactName").val(d.FactName);
                                $("#WithdrawChannelCode").val(d.WithdrawChannelCode);
                                $("#ProvinceId").val(d.ProvinceId);
                                $("#ReservedPhone").val(d.ReservedPhone);
                                loadCity(d.ProvinceId,d.CityId);
                                loadBank(d.WithdrawChannelCode, d.ProvinceId, d.CityId, d.BankLasalleCode);
                            }
                        }
                    });
                }

            }

        });


        $("#ProvinceId").change(function () {
            var pid = $.trim(this.value);
            if (pid == '') {
                $("#CityId").empty().append('<option value="">请选择</option>');
                return;
            }
            loadCity(pid);
            var _html = '<option value="">请选择</option>';
            $("#BankLasalleCode").empty().append(_html);
        });

        $("#CityId").change(function () {
            var pid = $.trim(this.value);
            if (pid == '') {
                $("#BankLasalleCode").empty().append('<option value="">请选择</option>');
                return;
            }
            var data = validate.serializeObject($("#formdata"));
            loadBank(data.WithdrawChannelCode, data.ProvinceId, data.CityId);
        });

        $("#WithdrawChannelCode").change(function () {
            var pid = $.trim(this.value);
            if (pid == '') {
                $("#BankLasalleCode").empty().append('<option value="">请选择</option>');
                return;
            }
            var data = validate.serializeObject($("#formdata"));
            if (data.WithdrawChannelCode != '' && data.ProvinceId != '' && data.CityId != '') {
                loadBank(data.WithdrawChannelCode, data.ProvinceId, data.CityId);
            }

        });

        var codeClick = false;

        $("#SendSmsCode").click(function () {
            if (codeClick) return;

            $.post("/DrawingApi/SendMobile",
                {
                    "Mobile": '@Model.MobilePhone',
                    "SmsKey": "smsagentwithdrawapply",
                    "SmsCode": "jiesuan"
                },
                function(data) {
                    if (data.IsSuccess) {
                        layer.msg('获取短信验证码成功', { icon: 1 });
                        codeClick = true;
                        var showText = '%s s重新获取';
                        var $this = $("#SendSmsCode");

                        if ($this.data('alt_button_disable_rendered') == 0) {
                            return false;
                        }
                        setTimeout(function () {
                            var count = 60;
                            setText();
                            $this.addClass("cur");
                            function setText() {
                                $this.html(showText.replace('%s', count));

                                if (count > 0) {
                                    count--;
                                    $this.data('alt_button_disable_rendered', 0);
                                    setTimeout(setText, 1000);
                                } else {
                                    codeClick = false;
                                    $this.html('重新获取');
                                    $this.data('alt_button_disable_rendered', 1);
                                }
                            }
                        }, 10);
                    } else {
                        layer.msg(data.message, { icon: 5 });
                        return false;
                    }
                });
        });

        $("#GetSmsCode").click(function () {

            if (codeClick) {
                return;
            }

            var imgCode = $.trim($("#ImgCode").val());
            if (imgCode == '') {
                layer.msg('请输入图片验证码', { icon: 5 });
                $('#ImgCode').focus();
                return;
            }

            if (imgCode.length != 4) {
                layer.msg('验证码输入错误', { icon: 5 });
                $('#ImgCode').focus();
                return;
            }


            //获取短信验证码
            $.post("/DrawingApi/SendMobileCode", { "Mobile": '@Model.MobilePhone', "SmsKey": "smsagentwithdrawapply", "ImgCode": imgCode, "ImgCodeKey": "imgagentwithdrawapply", SmsCode: "jiesuan" }, function (data) {
                if (data.IsSuccess) {
                    layer.msg('获取短信验证码成功', { icon: 5 });
                    codeClick = true;
                    var showText = '%s s重新获取';
                    var $this = $("#GetSmsCode");

                    if ($this.data('alt_button_disable_rendered') == 0) {
                        return false;
                    }
                    setTimeout(function () {
                        var count = 60;
                        setText();
                        $this.addClass("cur");
                        function setText() {
                            $this.html(showText.replace('%s', count));

                            if (count > 0) {
                                count--;
                                $this.data('alt_button_disable_rendered', 0);
                                setTimeout(setText, 1000);
                            } else {
                                codeClick = false;
                                $this.html('重新获取');
                                $this.data('alt_button_disable_rendered', 1);
                            }
                        }
                    }, 10);

                } else {
                    layer.msg(data.message, { icon: 5 });
                    return false;
                }

            });

            return;
        });


        $("#WithdrawAmt").blur(function () {
            var amount = $.trim($("#WithdrawAmt").val());
            getWithdrawHanding(amount);
        });


        var comt = true;

        //提现申请
        $("#btn_submit").click(function () {

            var data = validate.serializeObject($("#formdata"));

            if (data.BankCode == '') {
                layer.msg('请填写银行卡账号', { icon: 5 });
                $('#BankCode').focus();
                return false;
            }

            if (data.FactName == '') {
                layer.msg('请填写收款人姓名', { icon: 5 });
                $('#FactName').focus();
                return false;
            }

            //if (!validate.isBankCode(data.BankCode))
            //{
            //    layer.msg('银行卡号格式错误！', { icon: 5 });
            //    $('#BankCode').focus();
            //    return false;
            //}

            if (data.ReservedPhone != ''&&!validate.isMobile(data.ReservedPhone)) {
                layer.msg('预留手机号格式错误！', { icon: 5 });
                $('#ReservedPhone').focus();
                return false;
            }


            if (data.WithdrawChannelCode == '') {
                layer.msg('请选择开户行！', { icon: 5 });
                $('#WithdrawChannelCode').focus();
                return false;
            } else {
                data.BankName = $.trim($("#WithdrawChannelCode").find("option:selected").text());
                data.WithdrawChanneName = $.trim($("#WithdrawChannelCode").find("option:selected").text());
            }


            if (data.ProvinceId == '') {
                layer.msg('请选择开户行省！', { icon: 5 });
                $('#ProvinceId').focus();
                return false;
            } else {
                data.ProvinceName = $.trim($("#ProvinceId").find("option:selected").text());
            }
            if (data.CityId == '') {
                layer.msg('请选择开户行市！', { icon: 5 });
                $('#CityId').focus();
                return false;
            } else {
                data.CityName = $.trim($("#CityId").find("option:selected").text());
            }

            if (data.BankLasalleCode == '') {
                layer.msg('请选择开户支行！', { icon: 5 });
                $('#BankLasalleCode').focus();
                return false;
            } else {
                data.BankAddress = $.trim($("#BankLasalleCode").find("option:selected").text());
            }

            if (data.WithdrawAmt == '') {
                layer.msg('请输入提现金额！', { icon: 5 });
                $('#WithdrawAmt').focus();
                return false;
            }

            if (data.WithdrawAmt==0)
            {
                layer.msg('提现金额不能为0！', { icon: 5 });
                $('#WithdrawAmt').focus();
                return false;
            }

            var balance = parseFloat($.trim($("#Balance").val()));
            var withdrawAmt = parseFloat(data.WithdrawAmt);
            var handing = parseFloat(data.Handing);
            if (withdrawAmt + handing > balance) {

                layer.msg('余额不够，请修改提现金额', { icon: 5 });
                $('#WithdrawAmt').focus();
                return false;
            }
            if (data.Pass2 == '') {

                layer.msg('请填写提现密码', { icon: 5 });
                $('#Pass2').focus();
                return false;
            }

            if (data.PhoneCode == '') {

                layer.msg('请填写提现短信验证码', { icon: 5 });
                $('#PhoneCode').focus();

                return false;
            }

            if (comt)
            {
                comt = false;
                $.HM.ajax("Withdraw/WithdrawApplyIn", {
                    data: data,
                    type: "POST",
                    dataType: 'json',
                    success: function (d) {
                        if (d.IsSuccess) {
                            layer.msg(d.message, { icon: 1 });
                            setTimeout(function () {
                                window.location = window.location.href;
                            }, 1500);
                        } else {
                            comt = true;
                            layer.msg(d.message, { icon: 0 });
                        }
                    }
                });
            }


        });

    });
</script>